#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tz_error.h"
#include "tz_iimage_lib.h"
#include "tz_iarray.h"
#include "tz_arrayview.h"
#include "tz_image_lib_defs.h"
#include "tz_math.h"



IMatrix* Get_Int_Matrix3(const Stack *stack)
{
  IMatrix* dm = NULL;

  dim_type dim[3];
  dim[0] = (dim_type)stack->width;
  dim[1] = (dim_type)stack->height;
  dim[2] = (dim_type)stack->depth;

  dm = Make_IMatrix(dim,3);
  
  uint16* array16 = (uint16*) (stack->array);
  float32* array32 = (float32*) (stack->array);
  float64* array64 = (float64*) (stack->array);
  color_t *array_color = (color_t*) (stack->array);

  size_t i,length;
  length = (size_t) dim[0]*dim[1]*dim[2];//stack->width*stack->height*stack->depth;

  switch(stack->kind) {
  case GREY:
    for(i=0;i<length;i++)
      dm->array[i] = (int) (stack->array[i]);
    break;
  case GREY16:
    for(i=0;i<length;i++)
      dm->array[i] = (int) (array16[i]);   
    break;
  case FLOAT32:
    for(i=0;i<length;i++)
      dm->array[i] = (int) (array32[i]);
    break;
  case FLOAT64:
    for(i=0;i<length;i++)
      dm->array[i] = (int) (array64[i]);
    break;
  case COLOR:
    for(i=0;i<length;i++) {
      dm->array[i] = (int) (array_color[i][0]) + (int) (array_color[i][1]) + 
	(int) (array_color[i][2]);
    }
    break;
  default:
    fprintf(stderr,"Unrecongnzied stack kind in Get_Float_Matrix3");
  }

  return dm;  
}

